package com.mfl.controller.user;

import com.mfl.common.Result;
import com.mfl.pojo.dto.UserDTO;
import com.mfl.pojo.dto.UserLoginDTO;
import com.mfl.pojo.entity.User;
import com.mfl.pojo.vo.UserLoginVO;
import com.mfl.pojo.vo.UserVO;
import com.mfl.properties.JwtProperties;
import com.mfl.service.UserService;
import com.mfl.utils.JwtUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

@RestController("userUserController")
@RequestMapping("/cky/user/client")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private JwtProperties jwtProperties;

    /**
     * 用户登录
     * @return
     */
    @PostMapping("/login")
    public Result<UserLoginVO> login(@RequestBody UserLoginDTO userLoginDTO){
        log.info("用户登录：{}", userLoginDTO);
        User user = userService.login(userLoginDTO);
        // 生成jwt令牌
        HashMap<String, Object> claims = new HashMap<>();
        claims.put("userId", user.getId());
        String token = JwtUtil.createJwt(jwtProperties.getUserSecretKey(), jwtProperties.getUserExpire(), claims);
        UserLoginVO vo = UserLoginVO.builder()
                .id(user.getId())
                .name(user.getName())
                .avatar(user.getAvatar())
                .token(token)
                .build();
        return Result.success("登录成功", vo);
    }

    /**
     * 注册用户
     * @param userDTO
     * @return
     */
    @PostMapping("/register")
    public Result register(@RequestBody UserDTO userDTO){
        log.info("注册用户：{}", userDTO);
        userService.insert(userDTO);
        return Result.success("注册成功");
    }

    /**
     * 校验token是否过期
     *
     * @param token
     * @return
     */
    @GetMapping("/checkUserToken")
    public Result checkUserToken(@RequestParam String token) {
        try {
            JwtUtil.parseJwt(jwtProperties.getUserSecretKey(), token);
            log.info("checkUserToken：token有效");
        } catch (Exception e) {
            // 令牌已过期
            log.info("checkUserToken：toke过期");
            return Result.error(401, "expired");
        }
        return Result.success("", null);
    }

    /**
     * 根据id查询用户信息
     * @param id
     * @return
     */
    @GetMapping("/getUserById")
    public Result<UserVO> getUserById(int id) {
        log.info("根据id查询用户：{}", id);
        UserVO userVO = userService.getUserById(id);
        return Result.success("", userVO);
    }

    /**
     * 修改用户信息
     * @param userDTO
     * @return
     */
    @PutMapping("/update")
    public Result updateUserInfo(@RequestBody UserDTO userDTO){
        log.info("修改用户信息：{}", userDTO);
        userService.updateUserInfo(userDTO);
        return Result.success("修改成功");
    }

    /**
     * 校验密码
     * @param userInfoDTO
     * @return
     */
    @GetMapping("/checkPwd")
    public Result<Boolean> checkPwd(UserDTO UserDTO) {
        log.info("校验密码：{}", UserDTO);
        Boolean flag = userService.checkPwd(UserDTO);
        return Result.success("", flag);
    }

    /**
     * 修改密码
     * @param userDTO
     * @return
     */
    @PutMapping("/updatePwd")
    public Result updatePwd(@RequestBody UserDTO userDTO){
        log.info("修改密码：{}", userDTO);
        userService.updatePwd(userDTO);
        return Result.success("修改成功");
    }
}
